import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {HttpClient} from '@angular/common/http';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit {
  param = {
    pageNo: 1,
    pageSize: 10,
    count: 0
  };
  list = [];
  loadingFlag = true;
  isVisible = false;
  private deleteId;
  okLoading = false;

  constructor(
    private router: Router,
    private http: HttpClient,
    private message: NzMessageService
  ) {
  }

  ngOnInit() {
    this.loadData();
  }

  loadData() {
    this.loadingFlag = true;
    this.http.post('/api/initialization/listProject', this.param).subscribe(res => {
      this.list = res[`data`];
      this.param.count = res[`count`];
      this.loadingFlag = false;
    });
  }

  add() {
    this.router.navigate(['/project/new']);
  }

  pageChange($event: number) {
    this.param.pageNo = $event;
    this.loadData();
  }

  delete(id: any) {
    this.deleteId = id;
    this.isVisible = true;
  }

  okCallback() {
    this.okLoading = true;
    this.http.delete('/api/initialization/deleteProject/' + this.deleteId).subscribe(() => {
      this.isVisible = false;
      this.okLoading = false;
      this.loadData();
      this.message.success('删除成功', {
        nzDuration: 2000
      });
    });
  }
}
